{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import mosquitto\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "client = mosquitto.Mosquitto('Subscriber-%s' % os.getpid())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def on_connect(mosq, obj, rc):\n",
    "    if rc == 0:\n",
    "        print('Connected')\n",
    "    else:\n",
    "        print('Connection Error')\n",
    "client.on_connect = on_connect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def on_message(mosq, obj, msg):\n",
    "    print('Topic: %s' % msg.topic)\n",
    "    print('QoS: %s' % msg.qos)\n",
    "    print('Retain: %s' % msg.retain)\n",
    "    print('Payload: %s' % msg.payload)\n",
    "    client.unsubscribe('mqtt/example')\n",
    "client.on_message = on_message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def on_unsubscribe(mosq, obj, mid):\n",
    "    print(\"Unsubscribe with mid %s received.\" % mid)\n",
    "    client.disconnect()\n",
    "client.on_unsubscribe = on_unsubscribe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "client.connect(\"127.0.0.1\")\n",
    "client.subscribe(\"mqtt/example\", 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "while client.loop(timeout=1) == 0:\n",
    "    pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
